<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程安全集合对比 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            margin-bottom: 1rem;
        }
        .comparison-table th {
            font-weight: 600;
            background-color: #f8fafc;
        }
        .comparison-table tr:nth-child(even) {
            background-color: #f8fafc;
        }
        .pros-cons-item {
            transition: all 0.3s ease;
        }
        .pros-cons-item:hover {
            transform: scale(1.02);
        }
        .mermaid {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="max-w-6xl mx-auto">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Java线程安全集合深度解析</h1>
                    <p class="text-xl mb-8 opacity-90">探索CopyOnWriteArrayList与Collections.synchronizedList的核心差异与应用场景</p>
                    <div class="flex space-x-4">
                        <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full flex items-center">
                            <i class="fas fa-lock mr-2"></i> 线程安全
                        </span>
                        <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full flex items-center">
                            <i class="fas fa-bolt mr-2"></i> 高性能
                        </span>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="relative w-full max-w-md">
                        <div class="absolute inset-0 bg-white bg-opacity-20 rounded-xl transform rotate-6"></div>
                        <div class="relative bg-white bg-opacity-10 backdrop-filter backdrop-blur-lg rounded-xl p-6 shadow-lg">
                            <div class="mermaid">
                                graph LR
                                    A[线程安全集合] --> B[CopyOnWriteArrayList]
                                    A --> C[Collections.synchronizedList]
                                    B --> D[写时复制机制]
                                    B --> E[读高性能]
                                    B --> F[内存消耗大]
                                    C --> G[同步锁机制]
                                    C --> H[强一致性]
                                    C --> I[读写均衡]
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section class="py-16 px-4">
        <div class="max-w-6xl mx-auto">
            <!-- Introduction -->
            <div class="mb-16 text-center">
                <h2 class="text-3xl font-bold mb-6 text-gray-800">核心机制对比</h2>
                <p class="text-lg text-gray-600 max-w-3xl mx-auto">
                    Java的<code class="bg-gray-100 px-2 py-1 rounded">CopyOnWriteArrayList</code>和<code class="bg-gray-100 px-2 py-1 rounded">Collections.synchronizedList</code>都是线程安全的集合类，但它们的工作机制和适用场景有所不同。
                </p>
            </div>

            <!-- Mechanism Comparison -->
            <div class="grid md:grid-cols-2 gap-8 mb-16">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-all duration-300">
                    <div class="p-6">
                        <div class="feature-icon bg-blue-100 text-blue-600">
                            <i class="fas fa-copy text-2xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">CopyOnWriteArrayList</h3>
                        <p class="text-gray-600 mb-4">
                            采用<strong>写时复制</strong>(Copy-On-Write)的机制。当有写操作（如add、remove）时，会创建一个底层数组的副本，在新副本上进行操作，操作完成后将新副本替换为原数组。
                        </p>
                        <ul class="space-y-2 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>读操作不需要加锁，性能高</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>适合读多写少的场景</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>写操作性能较低，内存消耗大</span>
                            </li>
                        </ul>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-all duration-300">
                    <div class="p-6">
                        <div class="feature-icon bg-purple-100 text-purple-600">
                            <i class="fas fa-lock text-2xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">Collections.synchronizedList</h3>
                        <p class="text-gray-600 mb-4">
                            通过<strong>同步</strong>(synchronization)机制实现线程安全。将普通的List包装成一个线程安全的List，所有的读写操作都被加上了同步锁。
                        </p>
                        <ul class="space-y-2 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>保证强一致性</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>适合读写操作均衡的场景</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>高并发下性能较低</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Detailed Comparison -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">详细对比分析</h2>
                
                <div class="overflow-x-auto">
                    <table class="min-w-full bg-white rounded-lg overflow-hidden comparison-table">
                        <thead>
                            <tr class="text-left border-b border-gray-200">
                                <th class="py-4 px-6">对比项</th>
                                <th class="py-4 px-6">CopyOnWriteArrayList</th>
                                <th class="py-4 px-6">Collections.synchronizedList</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="border-b border-gray-200">
                                <td class="py-4 px-6 font-medium">工作机制</td>
                                <td class="py-4 px-6">写时复制机制，读操作无锁</td>
                                <td class="py-4 px-6">同步锁机制，所有操作加锁</td>
                            </tr>
                            <tr class="border-b border-gray-200">
                                <td class="py-4 px-6 font-medium">读性能</td>
                                <td class="py-4 px-6 text-green-600 font-medium">高 (无需加锁)</td>
                                <td class="py-4 px-6 text-yellow-600 font-medium">中 (需要获取锁)</td>
                            </tr>
                            <tr class="border-b border-gray-200">
                                <td class="py-4 px-6 font-medium">写性能</td>
                                <td class="py-4 px-6 text-red-600 font-medium">低 (需要复制数组)</td>
                                <td class="py-4 px-6 text-yellow-600 font-medium">中 (需要获取锁)</td>
                            </tr>
                            <tr class="border-b border-gray-200">
                                <td class="py-4 px-6 font-medium">一致性</td>
                                <td class="py-4 px-6">弱一致性 (读操作看到的是快照)</td>
                                <td class="py-4 px-6">强一致性 (总能读取最新数据)</td>
                            </tr>
                            <tr>
                                <td class="py-4 px-6 font-medium">内存消耗</td>
                                <td class="py-4 px-6">高 (写操作时复制数组)</td>
                                <td class="py-4 px-6">低 (与普通List相同)</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Pros & Cons -->
            <div class="grid md:grid-cols-2 gap-8 mb-16">
                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="bg-blue-50 px-6 py-4 border-b border-blue-100">
                        <h3 class="text-xl font-bold text-blue-800">CopyOnWriteArrayList</h3>
                    </div>
                    <div class="p-6">
                        <div class="mb-6">
                            <h4 class="text-lg font-semibold text-blue-700 mb-3 flex items-center">
                                <i class="fas fa-thumbs-up mr-2"></i> 优点
                            </h4>
                            <div class="space-y-3">
                                <div class="pros-cons-item bg-blue-50 rounded-lg p-4">
                                    <div class="font-medium text-blue-800">高并发读性能</div>
                                    <div class="text-sm text-blue-600">读操作不需要加锁，在高并发场景下可以并发执行</div>
                                </div>
                                <div class="pros-cons-item bg-blue-50 rounded-lg p-4">
                                    <div class="font-medium text-blue-800">无阻塞读取</div>
                                    <div class="text-sm text-blue-600">读操作无阻塞，适合读多写少的场景</div>
                                </div>
                                <div class="pros-cons-item bg-blue-50 rounded-lg p-4">
                                    <div class="font-medium text-blue-800">线程安全</div>
                                    <div class="text-sm text-blue-600">写操作通过复制机制保证线程安全</div>
                                </div>
                            </div>
                        </div>
                        <div>
                            <h4 class="text-lg font-semibold text-red-700 mb-3 flex items-center">
                                <i class="fas fa-thumbs-down mr-2"></i> 缺点
                            </h4>
                            <div class="space-y-3">
                                <div class="pros-cons-item bg-red-50 rounded-lg p-4">
                                    <div class="font-medium text-red-800">写性能低</div>
                                    <div class="text-sm text-red-600">每次写操作都会创建数组副本，性能较低</div>
                                </div>
                                <div class="pros-cons-item bg-red-50 rounded-lg p-4">
                                    <div class="font-medium text-red-800">内存消耗大</div>
                                    <div class="text-sm text-red-600">写操作时会产生大量的对象副本</div>
                                </div>
                                <div class="pros-cons-item bg-red-50 rounded-lg p-4">
                                    <div class="font-medium text-red-800">弱一致性</div>
                                    <div class="text-sm text-red-600">读操作可能看不到最新的数据</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="bg-purple-50 px-6 py-4 border-b border-purple-100">
                        <h3 class="text-xl font-bold text-purple-800">Collections.synchronizedList</h3>
                    </div>
                    <div class="p-6">
                        <div class="mb-6">
                            <h4 class="text-lg font-semibold text-purple-700 mb-3 flex items-center">
                                <i class="fas fa-thumbs-up mr-2"></i> 优点
                            </h4>
                            <div class="space-y-3">
                                <div class="pros-cons-item bg-purple-50 rounded-lg p-4">
                                    <div class="font-medium text-purple-800">强一致性</div>
                                    <div class="text-sm text-purple-600">每次读操作都能获取最新的数据</div>
                                </div>
                                <div class="pros-cons-item bg-purple-50 rounded-lg p-4">
                                    <div class="font-medium text-purple-800">读写均衡</div>
                                    <div class="text-sm text-purple-600">对于读写操作均衡的场景表现较好</div>
                                </div>
                                <div class="pros-cons-item bg-purple-50 rounded-lg p-4">
                                    <div class="font-medium text-purple-800">内存效率</div>
                                    <div class="text-sm text-purple-600">没有额外的内存消耗</div>
                                </div>
                            </div>
                        </div>
                        <div>
                            <h4 class="text-lg font-semibold text-red-700 mb-3 flex items-center">
                                <i class="fas fa-thumbs-down mr-2"></i> 缺点
                            </h4>
                            <div class="space-y-3">
                                <div class="pros-cons-item bg-red-50 rounded-lg p-4">
                                    <div class="font-medium text-red-800">性能受限</div>
                                    <div class="text-sm text-red-600">每次操作都需要获取锁，高并发下性能低</div>
                                </div>
                                <div class="pros-cons-item bg-red-50 rounded-lg p-4">
                                    <div class="font-medium text-red-800">线程争抢</div>
                                    <div class="text-sm text-red-600">可能导致较多的线程争抢锁</div>
                                </div>
                                <div class="pros-cons-item bg-red-50 rounded-lg p-4">
                                    <div class="font-medium text-red-800">阻塞操作</div>
                                    <div class="text-sm text-red-600">读写操作会阻塞其他线程的操作</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Use Cases -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-16">
                <div class="bg-gradient-to-r from-blue-500 to-purple-600 px-6 py-4">
                    <h3 class="text-xl font-bold text-white">适用场景</h3>
                </div>
                <div class="p-6">
                    <div class="grid md:grid-cols-2 gap-8">
                        <div>
                            <h4 class="text-lg font-semibold text-blue-700 mb-4 flex items-center">
                                <i class="fas fa-copy mr-2"></i> CopyOnWriteArrayList
                            </h4>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <span class="bg-blue-100 text-blue-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">1</span>
                                    <span class="text-gray-700">读操作频繁、写操作较少的场景</span>
                                </li>
                                <li class="flex items-start">
                                    <span class="bg-blue-100 text-blue-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">2</span>
                                    <span class="text-gray-700">缓存系统</span>
                                </li>
                                <li class="flex items-start">
                                    <span class="bg-blue-100 text-blue-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">3</span>
                                    <span class="text-gray-700">配置信息列表</span>
                                </li>
                                <li class="flex items-start">
                                    <span class="bg-blue-100 text-blue-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">4</span>
                                    <span class="text-gray-700">事件监听器列表</span>
                                </li>
                            </ul>
                        </div>
                        <div>
                            <h4 class="text-lg font-semibold text-purple-700 mb-4 flex items-center">
                                <i class="fas fa-lock mr-2"></i> Collections.synchronizedList
                            </h4>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">1</span>
                                    <span class="text-gray-700">读写操作均衡的场景</span>
                                </li>
                                <li class="flex items-start">
                                    <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">2</span>
                                    <span class="text-gray-700">写操作相对较多的场景</span>
                                </li>
                                <li class="flex items-start">
                                    <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">3</span>
                                    <span class="text-gray-700">需要强一致性的场景</span>
                                </li>
                                <li class="flex items-start">
                                    <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">4</span>
                                    <span class="text-gray-700">并发量不高的情况</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Performance Visualization -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">性能对比可视化</h2>
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <div class="mermaid">
                        %%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#f8fafc'}}}%%
                        xychart-beta
                            title "读写性能对比"
                            x-axis ["读操作", "写操作"]
                            y-axis "性能" 0 --> 100
                            bar [70, 20] --> "CopyOnWriteArrayList"
                            bar [40, 40] --> "Collections.synchronizedList"
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8 px-4">
        <div class="max-w-6xl mx-auto">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                    <p class="text-gray-400 mt-1">专注技术分享与学习</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-blue-100 transition-colors duration-200 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-gray-500 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: '"Noto Sans SC", sans-serif'
        });
    </script>
</body>
</html>